﻿<%@ WebHandler Language="C#" Class="wuliaohuizong_chart" %>

using System;
using System.Web;
using System.Data;
using KeaoBase;
using ServiceStack.Redis;
using System.Configuration;
using System.Collections.Generic;
using System.Text;

public class wuliaohuizong_chart : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        try
        {
            string uid = context.Request.QueryString["uid"];
            string kid = context.Request.QueryString["kid"];
            if (!Auth.UserLogined(uid, kid))
            {
                context.Response.Write("{\"x\":[],\"data\":[]}");
                return;
            }
            string key = KeaoUtils.GetMD5Str(context.Request.Url.OriginalString.Replace(uid,"cache").Replace(kid,"cache"));
            using (IRedisClient redis = RedisClientPool.GetReadOnlyRedisClient())
            {
                if (redis != null)
                {
                    if (redis.ContainsKey(key))
                    {
                        context.Response.Write(redis.GetValue(key));
                        return;
                    }
                }
            }
            string zhanDian = context.Request.QueryString["zhandian"];
            string chengZhongLeiXing = context.Request.QueryString["chengzhongleixing"];
            string kaiShiShiJian = context.Request.QueryString["kaishishijian"];
            string jieShuShiJian = context.Request.QueryString["jieshushijian"];
            if (string.IsNullOrEmpty(chengZhongLeiXing) || string.IsNullOrEmpty(zhanDian))
            {
                context.Response.Write("{\"x\":[],\"data\":[]}");
                return;
            }
            BaseDataBase m_DB = System.Reflection.Assembly.Load("KeaoDataBase").CreateInstance(string.Format("KeaoDataBase.{0}", ConfigurationManager.AppSettings["数据库类型"])) as BaseDataBase;
            if (!m_DB.InitDataBase(ConfigurationManager.ConnectionStrings["DataSource"].ConnectionString))
            {
                context.Response.Write("{\"x\":[],\"data\":[]}");
                return;
            }
            else
            {
                string createCondition = m_DB.CreateAndCondition("chengzhongleixing", DataValueCommonType.字符串, chengZhongLeiXing, "", SearchConditionType.等于);
                if (zhanDian != "—全部—")
                {
                    createCondition += m_DB.CreateAndCondition("zhandian", DataValueCommonType.字符串, zhanDian, "", SearchConditionType.等于);
                }
                if (!string.IsNullOrEmpty(kaiShiShiJian) && !string.IsNullOrEmpty(jieShuShiJian))
                {
                    if (chengZhongLeiXing == "入库物料")
                    {
                        createCondition += m_DB.CreateAndCondition("pizhongshijian", DataValueCommonType.日期时间, string.Format("{0} 00:00:00", kaiShiShiJian), string.Format("{0} 23:59:59", jieShuShiJian), SearchConditionType.between);
                    }
                    else
                    {
                        createCondition += m_DB.CreateAndCondition("maozhongshijian", DataValueCommonType.日期时间, string.Format("{0} 00:00:00", kaiShiShiJian), string.Format("{0} 23:59:59", jieShuShiJian), SearchConditionType.between);
                    }
                }
                string tableCols = string.Empty;
                string groupCols = string.Empty;
                Dictionary<string, string> orderBy = new Dictionary<string, string>();
                orderBy.Add("wuliaomingcheng", "asc");
                orderBy.Add("xinghao", "asc");
                tableCols = string.Format("wuliaomingcheng,xinghao,count(id) as cheshu,sum(maozhong) as maozhong,sum(pizhong) as pizhong,sum(kouzhong) as kouzhong,sum(jingzhong) as jingzhong,sum(fahuozhongliang) as fahuozhongliang,sum(jisuanfangliang) as jisuanfangliang", m_DB.DateFormatFun("maozhongshijian"));
                groupCols = string.Format("wuliaomingcheng,xinghao", m_DB.DateFormatFun("maozhongshijian"));
                DataTable Dt = m_DB.GetDataTableByCondition("tbl_chengzhong", tableCols, null, createCondition, groupCols, orderBy);

                if (Dt.Rows.Count > 0)
                {
                    StringBuilder xAxisBuilder = new StringBuilder();
                    StringBuilder seriesBuilder = new StringBuilder();

                    seriesBuilder.Append("\"data\":[");
                    xAxisBuilder.Append("\"x\":[");
                    foreach (DataRow row in Dt.Rows)
                    {
                        xAxisBuilder.Append(string.Format("\"{0}_{1}\",", row["wuliaomingcheng"], row["xinghao"]));
                        seriesBuilder.Append(string.Format("{0},", row["jingzhong"]));
                    }
                    seriesBuilder.Remove(seriesBuilder.Length - 1, 1);
                    xAxisBuilder.Remove(xAxisBuilder.Length - 1, 1);
                    seriesBuilder.Append("]");
                    xAxisBuilder.Append("]");
                    string responseJson = string.Format("{2}{0},{1}{3}", xAxisBuilder.ToString(), seriesBuilder.ToString(), "{", "}");
                    using (IRedisClient redis = RedisClientPool.GetRedisClient())
                    {
                        if (redis != null)
                        {
                            redis.SetValue(key, responseJson, TimeSpan.FromMinutes(10.0));
                        }
                    }
                    context.Response.Write(responseJson);

                }
                else
                {
                    context.Response.Write("{\"x\":[],\"data\":[]}");
                }
            }
        }
        catch (Exception err)
        {
            KeaoUtils.LogTxt(string.Format("物料汇总图表异常。{0}", err.Message));
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}